let React = require('react');
let SearchBox = require('./common/search-box.jsx');

var SearchResultList = React.createClass({
  getInitialState: function () {
    return {
      query: $.getQuery('q'),
      resultList: []
    };
  },

  componentDidMount: function () {
    var url = baseUrl + "getTextList/" + "/1/-1/-1/NULL/" + this.state.query;
    $.getJSON(url, function (result, status) {
      console.log(result.data);
      if (this.isMounted()) {
        this.setState({
          resultList: result.data
        });
      }
    }.bind(this));
  },

  render: function () {
    if ($.isBlank(this.state.query)) {
      return (
        <div></div>
      )
    } else {
      return (
        <div>
          <div className="devide-title"><h1>搜索结果</h1></div>
          <ul className="post-list">
            {this.state.resultList.map(function (post) {
            var href = 'post.html?id=' + post.id;
            return (
                <li key={post.id}>
                  <a href={href} target="_blank">{post.title}</a>
                  <div>
                    <span className="post-type">{post.text_type}</span>
                    <span className="post-time">发表时间：{post.update_date}</span>
                  </div>
                </li>
            )
          })}
          </ul>
        </div>
      );
    }
  }
});

var Search = React.createClass({
  render: function () {
    return (
      <div className="blog">
        <SearchBox/>
        <SearchResultList/>
      </div>
    );

  }
});

module.exports = Search;
